define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop/active/index' + location.search,
                    add_url: 'shop/active/add',
                    edit_url: 'shop/active/edit',
                    del_url: 'shop/active/del',
                    multi_url: 'shop/active/multi',
                    import_url: 'shop/active/import',
                    table: 'active',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='shop.name']", form).addClass("selectpage")
                    .data("source", "shop/shop/index")
                    .data("primaryKey", "name")
                    .data("field", "name")
                    .data("orderBy", "weigh desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh desc, id desc',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'shop.name', title: __('Shop_id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        //{field: 'describe', title: __('Describe'), operate: false},
                        {field: 'discount', title: __('Discount'), operate: false},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: statusList},
                        {field: 'new_up_time', title: __('New_up_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'new_down_time', title: __('New_down_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'up',
                                    icon: 'fa fa-arrow-up',
                                    title: '上架',
                                    text: '上架',
                                    extend: 'data-toggle="tooltip"',
                                    refresh: true,
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    url: 'shop/active/up_active',
                                    hidden: function(row, value, index){
                                        if(row.status == 0){
                                            return false;
                                        }
                                        return true;
                                    },
                                    confirm: "确定上架此项?"

                                },
                                {
                                    name: 'down',
                                    icon: 'fa fa-arrow-down',
                                    title: '下架',
                                    text: '下架',
                                    extend: 'data-toggle="tooltip"',
                                    refresh: true,
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    url: 'shop/active/down_active',
                                    hidden: function(row, value, index){
                                        if(row.status == 1){
                                            return false;
                                        }
                                        return true;
                                    },
                                    confirm: "确定下架此项?"
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});